%!TEX program = xelatex
\documentclass[lang=cn,11pt,a4paper]{elegantpaper}

% 文章标题区
\title{数学软件: Shell中变量的使用及利用参数混合语言编程}
\author{强基数学2001\\关博仁}
\date{\zhtoday}

% 文档命令区
\usepackage{array,url}
\newcommand{\code}[1]{\lstinline{#1}}

% 文档
\begin{document}

% 创建标题
\maketitle

%作业陈述
\section{简介}

本次作业当中复现参考资料中第30页的变量定义和脚本参数使用的Try it out，初步实现了混合C语言编程并且给出了一个例程，本报告使用模板\code{elegantpaper}, 源代码可见本文与我的仓库\code{https://gitee.com/wellsguan/mathematics-software/tree/master/Day2}.

\section{实验进程}

\subsection{教材代码复现}
此处为教材代码的微小修改版本
\begin{lstlisting}
#!/bin/sh
# 此文件与教材上相差不大，故正文不添加任何注释

var="Hello Everyone"
echo $var
echo "The program $0 is now running"
echo "The second parameter was $2"
echo "The var itself was $1"
echo "The parameter list was $*"
echo "Your home directory is $HOME"
echo "Please enter a new variable"
read var

echo "var has changed into $var, and the script is now complete"
exit 0

\end{lstlisting}

运行下面的脚本后
\begin{lstlisting}
bash run.sh

### run.sh:
chmod a+x try_var.sh
./try_var.sh do re mi
\end{lstlisting}

获得输出:
\begin{lstlisting}
Hello Everyone
The program ./try_var.sh is now running
The second parameter was re
The var itself was do
The parameter list was do re mi
Your home directory is /Users/phoolish
Please enter a new variable
fa so la xi
var has changed into fa so la xi, and the script is now complete
\end{lstlisting}

\subsection{实验设计思路}

脚本\code{try_var}的作用在于从命令行中读入三个参数后格式化输出，为此容易想到使用如下格式的命令替换
\begin{lstlisting}
variable='command'
variable=$(command)
\end{lstlisting}
就可以做一些利用脚本和C语言混合编程的简单实现，在本文当中，将要实现命令行输入字母大写小转换的功能，期望如下
\begin{lstlisting}
Input:  ./Trans abc
Output: ABC
\end{lstlisting}

\subsection{实验代码}
\code{Trans.c}文件见仓库或者\code{.zip}文件, 对应脚本文件如下
\begin{lstlisting}
#!/bin/sh

gcc src/Trans.c -o Trans

input=$1
echo $input
echo "The program $0 is now running"
echo "The input was $1"
output=`./Trans $input`
echo "The output was $output"
rm Trans
exit 0

\end{lstlisting}

\textbf{注意}：务必在Day2目录下运行\code{bash run.sh}进行测试, 测试结果有:

\begin{lstlisting}
bash run.sh

教材代码复现:
Hello Everyone
The program ./src/try_var.sh is now running
The second parameter was re
The var itself was do
The parameter list was do re mi
Your home directory is /Users/phoolish
Please enter a new variable
fo so la xi
var has changed into fo so la xi, and the script is now complete
测试实验代码, 请输入一个新字符串, 它将会转换大小写后输出
10YuanRMB
10YuanRMB
The program src/trans.sh is now running
The input was 10YuanRMB
The output was 10yUANrmb
\end{lstlisting}

\section{小结}
即使本文仅是一些脚本语言的简单使用，但是从命令行传参这点来看，Shell的强大之处可见一斑，可以轻松的现实多语言混合编程，在以后的工作中善用Shell语言可以简单实现如Python，C++等多语言的混用并且完全考虑代码编写时的结构问题，因为其本质链接可执行文件的方式从根源上避免了这些问题

\end{document}
